Option Strict On

Imports System.IO
Imports System.Runtime.Serialization
Imports System.Runtime.Serialization.Formatters.Binary

Namespace xpComponent.Serial
    <Serializable()> Public Class BaseClient
        Private mdtmDOB As Date
        Private mstrFirstName As String

        Public Sub New(ByVal pFName As String, ByVal pDOB As Date)
            mstrFirstName = pFName
            mdtmDOB = pDOB
        End Sub

        Public ReadOnly Property Age() As Long
            Get
                Return DateDiff(DateInterval.Year, mdtmDOB, Now)
            End Get
        End Property

        Public ReadOnly Property FirstName() As String
            Get
                Return mstrFirstName
            End Get
        End Property

        Public Overrides Function ToString() As String
            Return FirstName & " is " & Age.ToString & "."
        End Function
    End Class

    Public Class KeyRegistrySerial
        Private Shared _key As String = ""
        Private Shared _name As String = ""
        Private Shared _value As String = ""

        Public Sub New()
            Me._key = "Key"
            Me._name = "Name"
            Me._value = "Value"

        End Sub

        Public Shared Property Key() As String
            Get
                Return _key
            End Get
            Set(ByVal Value As String)
                _key = Value
            End Set
        End Property

        Public Shared Property Name() As String
            Get
                Return _name
            End Get
            Set(ByVal Value As String)
                _name = Value
            End Set
        End Property

        Public Shared Property Value() As String
            Get
                Return _value
            End Get
            Set(ByVal Value As String)
                _value = Value
            End Set
        End Property

        Public Shared Sub WriteRegistry(ByVal key As String)
            Dim oReg As Microsoft.Win32.RegistryKey

            'Opens the HKEY_LOCAL_MACHINE\Software key
            oReg = Microsoft.Win32.Registry.LocalMachine.OpenSubKey("Software", True)

            'Creates a sub key
            oReg.CreateSubKey(key)

            'Writes the value of the textboxes
            oReg = Microsoft.Win32.Registry.LocalMachine.OpenSubKey("Software\\" & key, True)
            oReg.SetValue(_name, _value)

            'Close the registry keys.
            oReg.Close()
        End Sub

        Public Shared Sub WriteRegistry(ByVal key As String, ByVal name As String)
            Dim oReg As Microsoft.Win32.RegistryKey

            'Opens the HKEY_LOCAL_MACHINE\Software key
            oReg = Microsoft.Win32.Registry.LocalMachine.OpenSubKey("Software", True)

            'Creates a sub key
            oReg.CreateSubKey(key)

            'Writes the value of the textboxes
            oReg = Microsoft.Win32.Registry.LocalMachine.OpenSubKey("Software\\" & key, True)
            oReg.SetValue(name, _value)

            'Close the registry keys.
            oReg.Close()
        End Sub

        Public Shared Sub WriteRegistry(ByVal key As String, ByVal name As String, ByVal value As String)
            Dim oReg As Microsoft.Win32.RegistryKey

            'Opens the HKEY_LOCAL_MACHINE\Software key
            oReg = Microsoft.Win32.Registry.LocalMachine.OpenSubKey("Software", True)

            'Creates a sub key
            oReg.CreateSubKey(key)

            'Writes the value of the textboxes
            oReg = Microsoft.Win32.Registry.LocalMachine.OpenSubKey("Software\\" & key, True)
            oReg.SetValue(name, value)

            'Close the registry keys.
            oReg.Close()
        End Sub

        Public Shared Sub ReadRegistry(ByVal key As String)
            Dim oReg As Microsoft.Win32.RegistryKey
            Dim strValue As String

            'Opens the key
            oReg = Microsoft.Win32.Registry.LocalMachine.OpenSubKey("Software\\" & key)
            'Validates that the key exists
            If oReg Is Nothing Then
                System.Windows.Forms.MessageBox.Show("The key Software\\" & key & " does not exist!", "Invalid key", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Error)
                Exit Sub
            End If

            'Read the stored value 
            strValue = oReg.GetValue(_name, "DEFAULT-VALUE").ToString
            _value = strValue
            'System.Windows.Forms.MessageBox.Show(strValue, "Stored Registry Value", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Information)

            'Close the registry keys.
            oReg.Close()
        End Sub

        Public Shared Sub GetValues(ByVal key As String)
            Dim oReg As Microsoft.Win32.RegistryKey

            'Opens the key
            oReg = Microsoft.Win32.Registry.LocalMachine.OpenSubKey("Software\\" & key)
            'Validates that the key exists
            If oReg Is Nothing Then
                System.Windows.Forms.MessageBox.Show("The key Software\\" & key & " does not exist!", "Invalid key", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Error)
                Exit Sub
            End If

            'Read the complete hierarchy from that key 
            RecursiveSearch("Software\\" & key, _value)

            'Displays the hierarchy
            'System.Windows.Forms.MessageBox.Show(_value)

            'Close the registry keys.
            oReg.Close()
        End Sub

        Private Shared Sub RecursiveSearch(ByVal pstrKey As String, ByRef pstrMessage As String)
            Dim oReg As Microsoft.Win32.RegistryKey

            'Opens the key
            oReg = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(pstrKey)

            pstrMessage &= pstrKey & Environment.NewLine

            'Read all values
            For Each x As String In oReg.GetValueNames()
                pstrMessage &= "        " & x & " = " & oReg.GetValue(x).ToString & Environment.NewLine
            Next

            'Read all subkeys
            For Each y As String In oReg.GetSubKeyNames
                'call the method again to loop through sub keys
                RecursiveSearch(pstrKey & "\\" & y, pstrMessage)
            Next
        End Sub

        Public Shared Sub DeleteRegistry(ByVal key As String)
            Dim oReg As Microsoft.Win32.RegistryKey

            'Opens the key
            oReg = Microsoft.Win32.Registry.LocalMachine.OpenSubKey("Software", True)

            'Delete the key.
            oReg.DeleteSubKeyTree(key)

            'Close the registry keys.
            oReg.Close()
        End Sub

        Public Shared Sub GetFileType(ByVal FileExtension As String)
            Dim oReg As Microsoft.Win32.RegistryKey
            Dim strIndexerKey As String
            Dim strType As String

            'Opens the key to retrieve the class name
            oReg = Microsoft.Win32.Registry.ClassesRoot.OpenSubKey(FileExtension)
            If oReg Is Nothing Then
                System.Windows.Forms.MessageBox.Show("The file extension " & FileExtension & " is unknown.", "File Type", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Information)
                Exit Sub
            End If
            strIndexerKey = oReg.GetValue("").ToString

            'Opens the class name to retrieve the file type
            oReg = Microsoft.Win32.Registry.ClassesRoot.OpenSubKey(strIndexerKey)
            _value = oReg.GetValue("", "").ToString
            'System.Windows.Forms.MessageBox.Show("The file extension " & FileExtension & " is a " & strType, "File Type", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Information)

            'Close the registry keys.
            oReg.Close()
        End Sub
    End Class

    <Serializable()> Public Class AdvancedSource
        Implements System.Runtime.Serialization.ISerializable

        Private mdtmDOB As Date
        Private mstrFirstName As String
        Private mstrLastName As String

        Public Sub New(ByVal pLName As String, ByVal pFName As String, ByVal pDOB As Date)
            mstrLastName = pLName
            mstrFirstName = pFName
            mdtmDOB = pDOB
        End Sub

        Public ReadOnly Property Age() As Long
            Get
                Return DateDiff(DateInterval.Year, mdtmDOB, Now)
            End Get
        End Property

        Public ReadOnly Property FirstName() As String
            Get
                Return mstrFirstName
            End Get
        End Property

        Public ReadOnly Property LastName() As String
            Get
                Return mstrLastName
            End Get
        End Property

        Public Overrides Function ToString() As String
            Return FirstName & " " & LastName & " is " & Age.ToString & "."
        End Function

        Public Sub GetObjectData(ByVal Info As System.Runtime.Serialization.SerializationInfo, ByVal Context As System.Runtime.Serialization.StreamingContext) Implements System.Runtime.Serialization.ISerializable.GetObjectData
            With Info
                .FullTypeName = "xpControls.Serial.AdvancedDestination"
                .AddValue("FullName", FirstName & " " & LastName)
                .AddValue("Age", Age)
            End With
        End Sub
    End Class

    <Serializable()> Public Class AdvancedDestination
        Implements System.Runtime.Serialization.ISerializable

        Private mintAge As Integer
        Private mstrFullName As String

        Public ReadOnly Property Age() As Long
            Get
                Return mintAge
            End Get
        End Property

        Public ReadOnly Property FullName() As String
            Get
                Return mstrFullName
            End Get
        End Property

        Public Overrides Function ToString() As String
            Return FullName & " is " & Age.ToString & "."
        End Function

        Private Sub New(ByVal Info As System.Runtime.Serialization.SerializationInfo, ByVal Context As System.Runtime.Serialization.StreamingContext)
            With Info
                mstrFullName = .GetString("FullName")
                mintAge = .GetInt32("Age")
            End With
        End Sub

        Public Sub GetObjectData(ByVal Info As System.Runtime.Serialization.SerializationInfo, ByVal Context As System.Runtime.Serialization.StreamingContext) Implements System.Runtime.Serialization.ISerializable.GetObjectData
            'With Info
            '    .AddValue("FullName", FullName)
            '    .AddValue("Age", Age)
            'End With
        End Sub
    End Class

    <Serializable()> Public Class SerializeWithChild
        Private mdtmDOB As Date
        Private mstrFirstName As String
        Private mstrLastName As String
        Private mcolChildren As System.Collections.Hashtable

        Public Sub New(ByVal pLName As String, ByVal pFName As String, ByVal pDOB As Date)
            mstrLastName = pLName
            mstrFirstName = pFName
            mdtmDOB = pDOB
        End Sub

        Public ReadOnly Property Age() As Long
            Get
                Return DateDiff(DateInterval.Year, mdtmDOB, Now)
            End Get
        End Property

        Public ReadOnly Property FirstName() As String
            Get
                Return mstrFirstName
            End Get
        End Property

        Public ReadOnly Property LastName() As String
            Get
                Return mstrLastName
            End Get
        End Property

        Public Overrides Function ToString() As String
            If IsNothing(mcolChildren) Then
                Return FirstName & " " & LastName & " is " & Age.ToString & "." & Environment.NewLine & "He (she) has no child."
            Else
                Return FirstName & " " & LastName & " is " & Age.ToString & "." & Environment.NewLine & "He (she) has " & mcolChildren.Count.ToString & " child(ren)."
            End If
        End Function

        Public Function Clone() As SerializeWithChild
            Dim ms As System.IO.Stream = New System.IO.MemoryStream
            Dim bf As System.Runtime.Serialization.Formatters.Binary.BinaryFormatter = New System.Runtime.Serialization.Formatters.Binary.BinaryFormatter

            bf.Serialize(ms, Me)
            ms.Position = 0  'Reset the position to the beginning of the stream
            Return CType(bf.Deserialize(ms), SerializeWithChild)
        End Function

        Public Sub AddChild(ByVal pFName As String, ByVal pDOB As Date)
            If IsNothing(mcolChildren) Then mcolChildren = New System.Collections.Hashtable
            Dim objChild As BaseClient = New BaseClient(pFName, pDOB)
            mcolChildren.Add(pFName, objChild)
        End Sub
    End Class

    <Serializable()> Public Class SerializationSimple
        Private mdtmDOB As Date
        Private mstrFirstName As String
        Private mstrLastName As String

        Public Sub New(ByVal pLName As String, ByVal pFName As String, ByVal pDOB As Date)
            mstrLastName = pLName
            mstrFirstName = pFName
            mdtmDOB = pDOB
        End Sub

        Public ReadOnly Property Age() As Long
            Get
                Return DateDiff(DateInterval.Year, mdtmDOB, Now)
            End Get
        End Property

        Public ReadOnly Property FirstName() As String
            Get
                Return mstrFirstName
            End Get
        End Property

        Public ReadOnly Property LastName() As String
            Get
                Return mstrLastName
            End Get
        End Property

        Public Overrides Function ToString() As String
            Return FirstName & " " & LastName & " is " & Age.ToString & "."
        End Function

        Public Function Clone() As SerializationSimple
            Dim ms As System.IO.Stream = New System.IO.MemoryStream
            Dim bf As System.Runtime.Serialization.Formatters.Binary.BinaryFormatter = New System.Runtime.Serialization.Formatters.Binary.BinaryFormatter

            bf.Serialize(ms, Me)
            ms.Position = 0  'Reset the position to the beginning of the stream
            Return CType(bf.Deserialize(ms), SerializationSimple)
        End Function
    End Class
End Namespace